package com.xinqi.common.rocketmq.dto;

import lombok.Data;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.UUID;

/**
 * MQ延时消息
 *
 * @author linkai
 * @date 2023/11/18
 */
@Data
public class MqDelayMessage implements Serializable {
    /**
     * 消息ID
     */
    private String id;
    /**
     * key
     */
    private String key;
    /**
     * tag
     */
    private String tag;
    /**
     * 生产时间
     */
    private LocalDateTime productionTime;
    /**
     * 到达时间戳(毫秒)
     */
    private Long arrivalTimeMilli;
    /**
     * 主题
     */
    private String topic;
    /**
     * 消息体
     */
    private String body;
    /**
     * 循环次数
     */
    private Integer cycleTimes;

    public void addCycleTimes() {
        if (this.cycleTimes == null) {
            this.cycleTimes = 1;
        } else {
            this.cycleTimes += 1;
        }
    }

    public static MqDelayMessage of(String key, String tab, String topic, String body, Long arrivalTimeMilli) {
        MqDelayMessage message = new MqDelayMessage();
        message.setId(UUID.randomUUID().toString());
        message.setKey(key);
        message.setTag(tab);
        message.setProductionTime(LocalDateTime.now());
        message.setArrivalTimeMilli(arrivalTimeMilli);
        message.setTopic(topic);
        message.setBody(body);
        message.setCycleTimes(0);
        return message;
    }

}
